package com.tencreat.service.account.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.constant.TencreatIdAndName;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.domain.account.MallRegion;
import com.tencreat.mapper.account.MallRegionMapper;
import com.tencreat.service.account.IMallRegionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域 Service业务层处理
 *
 * @author tencreat
 * @date 2021-09-17
 */
@Service
public class MallRegionServiceImpl extends ServiceImpl<MallRegionMapper, MallRegion> implements IMallRegionService {

    @Override
    public HttpResult<List<TencreatIdAndName>> getProvince() {
        List<MallRegion> regionList = lambdaQuery().eq(MallRegion::getPid, 0).list();
        List<TencreatIdAndName> tencreatIdAndNameList = new ArrayList<>();
        for (MallRegion mallRegion : regionList) {
            TencreatIdAndName tencreatIdAndName = new TencreatIdAndName();
            tencreatIdAndName.setId(mallRegion.getId());
            tencreatIdAndName.setName(mallRegion.getName());
            tencreatIdAndNameList.add(tencreatIdAndName);
        }
        return HttpResult.ok(tencreatIdAndNameList);
    }

    @Override
    public HttpResult<List<TencreatIdAndName>> getCityAndCounty(ReqIdParam reqIdParam) {
        List<MallRegion> regionList = lambdaQuery().eq(MallRegion::getPid, reqIdParam.getId()).list();
        List<TencreatIdAndName> tencreatIdAndNameList = new ArrayList<>();
        for (MallRegion mallRegion : regionList) {
            TencreatIdAndName tencreatIdAndName = new TencreatIdAndName();
            tencreatIdAndName.setId(mallRegion.getId());
            tencreatIdAndName.setName(mallRegion.getName());
            tencreatIdAndNameList.add(tencreatIdAndName);
        }
        return HttpResult.ok(tencreatIdAndNameList);
    }

    @Override
    public HttpResult<List<TencreatIdAndName>> getCity(ReqIdParam reqIdParam) {
        List<MallRegion> regionList = lambdaQuery().eq(MallRegion::getId, reqIdParam.getId()).list();
        List<TencreatIdAndName> tencreatIdAndNameList = new ArrayList<>();
        for (MallRegion mallRegion : regionList) {
            TencreatIdAndName tencreatIdAndName = new TencreatIdAndName();
            tencreatIdAndName.setId(mallRegion.getId());
            tencreatIdAndName.setName(mallRegion.getName());
            tencreatIdAndNameList.add(tencreatIdAndName);
        }
        return HttpResult.ok(tencreatIdAndNameList);
    }


    /******
     * 根据县级获取他们的所有上级并合并
     * @param id  县ID
     * @return
     */
    @Override
    public String getNameBycounty(Long id) {
        return baseMapper.getNameBycounty(id);
    }

    @Override
    public String getCounty(Long county) {
        return baseMapper.getCounty(county);
    }


    /**
     * 分页查询
     *
     * @param iPage      分页参数
     * @param mallRegion 查询条件
     * @return 分页结果
     */
    @Override
    public IPage<MallRegion> page(IPage<?> iPage, MallRegion mallRegion) {
        return baseMapper.page(iPage, mallRegion);
    }


    @Override
    public Map<String, List<MallRegion>> regionList(MallRegion mallRegion) {
        HashMap<String, List<MallRegion>> regionList = new HashMap<>();
        if (StringUtils.isNotEmpty(mallRegion.getName())) {
            ArrayList<MallRegion> newRegion = baseMapper.selectByName(mallRegion.getName());
            regionList.put(mallRegion.getName(), newRegion);
            return regionList;
        } else {
            char ch;
            for (ch = 'A'; ch <= 'Z'; ++ch) {
                String str = String.valueOf(ch);
                List<MallRegion> regions = baseMapper.selectByFirstPinYin(str);
                regionList.put(str, regions);
            }
            return regionList;
        }
    }
}



